package com.database.objects;

import java.io.Serializable;
import java.util.*;

public abstract class Element<Parent extends Element<?, ?>, Child extends Element<?, ?>> implements Serializable, Comparable<Element<?, ?>> {

	private static final long serialVersionUID = -2095236612827482496L;

	public abstract Status getStatus();

	public abstract void setStatus(Status status);

	public boolean isAddStatus() {
		return getStatus().isAdd();
	}

	public boolean isModifyStatus() {
		return getStatus().isModify();
	}

	public boolean isDeleteStatus() {
		return getStatus().isDelete();
	}

	public boolean isNothingStatus() {
		return getStatus().isUnknown();
	}

	public void markAsAdd() {
		setStatus(Status.ADD);
	}

	public void markAsDeleted() {
		setStatus(Status.DELETE);
	}

	public void markAsUnknown() {
		setStatus(Status.UNKNOWN);
	}

	public boolean isValid() {
		return isAddStatus() || isModifyStatus() || isNothingStatus();
	}

	public abstract String getLabel();

	public abstract Parent getParent();

	public abstract void setParent(Element<?, ?> parent);

	public abstract List<Child> getChilds();

	public abstract void addChild(Child child);

	public int getNumberChilds() {
		return getChilds().size();
	}

	public boolean hasChilds() {
		return !getChilds().isEmpty();
	}

	public Child getChildByLabel(String label) {
		for (Child child : getChilds()) {
			if (child.getLabel().equals(label)) {
				return child;
			}
		}
		return null;
	}

	public Project getProject() {
		return getParent().getProject();
	}

	public VersionRepository getVersionRepository() {
		return getProject().getVersionRepository();
	}

	public String getUrlVersionRepository() {
		return getVersionRepository().getUrl();
	}

	public abstract Type getType();

	@Override
	public int compareTo(Element<?, ?> other) {
		return getLabel().compareTo(other.getLabel());
	}

	public static List<Element<?, ?>> convertToListOfElements(Collection<Commit> commits) {
		List<Element<?, ?>> elements = new ArrayList<Element<?, ?>>();
		for (Commit commit : commits) {
			elements.add(commit);
		}
		return elements;
	}
	
	public String getDate(){
		return getParent().getDate();
	}

}